﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LaserController : MonoBehaviour
{
    private Hashtable targetTable = new Hashtable();
    // Start is called before the first frame update
    void Start()
    {
        targetTable = new Hashtable();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 scale = transform.localScale;
        scale += 2.0f * Time.deltaTime * new Vector3(1, 1, 1);
        transform.localScale = scale;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("enter");
        GameObject target = collision.gameObject;
        targetTable.Add(target.GetHashCode(), target);        
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        Debug.Log("enter");
        GameObject target = collision.gameObject;
        targetTable.Remove(target.GetHashCode());
    }

    public void hurtTargetTable()
    {
        List<GameObject> tmpList = new List<GameObject>();
        foreach (DictionaryEntry entry in targetTable)
        {
            GameObject target = entry.Value as GameObject;
            tmpList.Add(target);
        }
        Debug.Log(tmpList.Count);
        for (int i = 0; i < tmpList.Count; ++i)
        {
            tmpList[i].SendMessage("GetHurt", 50);
            if (tmpList[i].tag == "tree") {
                tmpList[i].GetComponent<FutureTreeController>().StartBurn();
            }
        }
        transform.localScale = new Vector3(0, 0, 0);
        enabled = false;
    }
}
